<?php
/**
 * Copyright 2013 Marek Kaszewski
 *
 * This file is part of the JHUB1 Online Community Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace Jhub1_Common\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Jhub1_Common\Model\SettingsObjects\UserLocalizationObject;
use Jhub1_Common\Model\SettingsObjects\UserConfigurationObject;
use Jhub1_Common\Model\SettingsObjects\UserConsoleSettingsObject;
use Jhub1_Common\Model\SettingsObjects\DateTimeObject;

/**
 * @ORM\Entity
 * @ORM\Table(name="jhoUsers", indexes={@ORM\Index(name="uuid_index", columns={"id", "resourceid"})})
 */
class Users extends POBase
{

    /**
     * @ORM\OneToMany(targetEntity="Agents", mappedBy="user", cascade={"persist","remove"})
     * @param \Doctrine\Common\Collections\Collection $property
     */
    private $agents;

    /**
     * @ORM\OneToMany(targetEntity="EndpointTypes", mappedBy="user", cascade={"persist","remove"})
     * @param \Doctrine\Common\Collections\Collection $property
     */
    private $endpointtype;

    /**
     * @ORM\Column(type="integer", nullable=false)
     * @var string
     */
    private $usertype;

    /**
     * @ORM\Column(type="string", length=8, unique=true, nullable=false)
     * @var string
     */
    private $groupid;

    /**
     * @ORM\Column(type="string", length=100, unique=true, nullable=false)
     * @var string
     */
    private $email;

    /**
     * @ORM\Column(type="string", length=100, nullable=true)
     * @var string
     */
    private $avatar;

    /**
     * @ORM\Column(type="string", length=250, nullable=false)
     * @var string
     */
    private $password;

    /**
     * @ORM\Column(type="string", length=100, unique=true, nullable=true)
     * @var string
     */
    private $name;

    /**
     * @ORM\Column(type="string", length=100, unique=true, nullable=true)
     * @var string
     */
    private $surname;

    /**
     * @ORM\Column(type="integer", nullable=false)
     * @var integer
     */
    private $status;

    /**
     * @ORM\Column(type="string", length=50, nullable=true)
     * @var string
     */
    private $resendactivationcode;

    /**
     * @ORM\Column(type="string", length=100)
     * @var string
     */
    private $activationcode;

    /**
     * @ORM\Column(type="string", length=255)
     * @var string
     */
    private $localization;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     * @var string
     */
    private $resourceid;

    /**
     * @ORM\Column(type="text")
     * @var string
     */
    private $configuration;

    /**
     * @ORM\Column(type="text", name="consolesettings",  nullable=true)
     * @var string
     */
    private $consoleSettings;

    /**
     * @ORM\Column(type="string", length=20, nullable=true)
     * @var string
     */
    private $lastlogin;

    public function __construct()
    {
        $this->agents = new \Doctrine\Common\Collections\ArrayCollection();
        $this->endpointtype = new \Doctrine\Common\Collections\ArrayCollection();
        $this->setCreated(new DateTimeObject());
    }

    /**
     * @param Agents $agent
     */
    public function addAgent(Agents $agent)
    {
        $agent->setUser($this);
        $this->agents->add($agent);
    }

    /** @return Collection */
    public function getAgents()
    {
        return $this->agents;
    }

    public function getAgent($id)
    {
        $retVal = null;
        foreach ($this->agents as $ag) {
            if ($ag->getId() == $id) {
                $retVal = $ag;
            }
        }
        return $retVal;
    }

    public function getEndpointTypes()
    {
        return $this->endpointtype;
    }

    public function getResendActivationCode()
    {
        return $this->resendactivationcode;
    }

    public function setResendActivationCode($val)
    {
        $this->resendactivationcode = $val;
    }

    public function getUserType()
    {
        return $this->usertype;
    }

    public function setUserType($val)
    {
        $this->usertype = $val;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setName($val)
    {
        $this->name = $val;
    }

    public function getAvatar()
    {
        return $this->avatar;
    }

    public function setAvatar($val)
    {
        $this->avatar = $val;
    }

    public function setSurname($val)
    {
        $this->surname = $val;
    }

    public function getSurname()
    {
        return $this->surname;
    }

    public function setResourceId($val)
    {
        $this->resourceid = $val;
    }

    public function getResourceID()
    {
        return $this->resourceid;
    }

    public function getGroupId()
    {
        return $this->groupid;
    }

    public function setGroupId($val)
    {
        $this->groupid = $val;
    }

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($val)
    {
        $this->email = $val;
    }

    public function getPassword()
    {
        return $this->password;
    }

    public function setPassword($val)
    {
        $this->password = $val;
    }

    public function getActivationCode()
    {
        return $this->activationcode;
    }

    public function setActivationCode($val)
    {
        $this->activationcode = $val;
    }

    public function getStatus()
    {
        return $this->status;
    }

    public function setStatus($val)
    {
        $this->status = $val;
    }

    public function getLastLogin()
    {
        $dateTime = new DateTimeObject();
        $dateTime->fromTimestamp($this->lastlogin);
        return $dateTime;
    }

    public function setLastLogin(DateTimeObject $date)
    {
        $this->lastlogin = $date->toTimestamp();
    }

    public function getConfiguration()
    {
        $configuration = new UserConfigurationObject();
        $configuration->fromJSON($this->configuration);
        return $configuration;
    }

    public function setConfiguration(UserConfigurationObject $configurationObject)
    {
        $this->configuration = $configurationObject->toJSON();
    }

    public function getLocalization()
    {
        $localization = new UserLocalizationObject();
        $localization->fromJSON($this->localization);
        return $localization;
    }

    public function setLocalization(UserLocalizationObject $localizationObject)
    {
        $this->localization = $localizationObject->toJSON();
    }

    public function getConsoleSettings()
    {
        $consoleSettings = new UserConsoleSettingsObject();
        $consoleSettings->fromJSON($this->consoleSettings);
        return $consoleSettings;
    }

    public function setConsoleSettings(UserConsoleSettingsObject $consoleSettings)
    {
        $this->consoleSettings = $consoleSettings->toJSON();
    }

    public function toString()
    {
        $string           = '---------------------------------------------'. PHP_EOL;
        $string = $string . 'USER OBJECT:          ' . PHP_EOL;
        $string = $string . '---------------------------------------------'. PHP_EOL;
        $string = $string . 'ID:              ' . $this->getId() . PHP_EOL;
        $string = $string . 'Email:           ' . $this->getEmail() . PHP_EOL;
        $string = $string . 'Name:            ' . $this->getName() . PHP_EOL;
        $string = $string . 'Surname:         ' . $this->getSurname() . PHP_EOL;
        $string = $string . 'GroupID:         ' . $this->getGroupId() . PHP_EOL;
        $string = $string . 'Type:            ' . $this->getUserType() . PHP_EOL;
        $string = $string . 'Status:          ' . $this->getStatus() . PHP_EOL;
        $string = $string . 'Created:         ' . PHP_EOL . $this->getCreated()->toString() . PHP_EOL;
        $string = $string . 'Updated:         ' . PHP_EOL . $this->getUpdated()->toString() . PHP_EOL;
        $string = $string . 'Last login:      ' . PHP_EOL . $this->getLastLogin()->toString() . PHP_EOL;
        $string = $string . 'Localization:    ' . PHP_EOL . $this->getLocalization()->toString() . PHP_EOL;
        $string = $string . 'Activation code: ' . $this->getActivationCode() . PHP_EOL;
        $string = $string . 'Rs-Activ code:   ' . $this->getResendActivationCode() . PHP_EOL;
        return $string;
    }

}
